limsPurchase.detailPage = {
    getBtnToolbar: function (id, step) {
        var _this = this
        var buttons = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                workspace.window.goBack()
            }
        },]
        if (step) {
            if (step === 'edit') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit', color: 'success',
                    onClick: function () {
                        _this.submitSingle(id)
                    }
                })
            } else if (step === 'audit') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick: function () {
                        _this.passSingle(id)
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick: function () {
                        _this.rejectSingle(id)
                    }
                })
            } else {

            }
        }
        return {
            type: 'btnToolbar',
            items: buttons
        }
    },

    submitSingle: function (id) {
        var _this = this
        Gikam.create('workflow').submit({
            data: [{id: id}],
            pageObject: limsPurchase,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    passSingle: function (id) {
        var _this = this
        Gikam.create('workflow').pass({
            data: [{id: id}],
            pageObject: limsPurchase,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    rejectSingle: function (id) {
        var _this = this
        Gikam.create('workflow').reject({
            data: [{id: id}],
            pageObject: limsPurchase,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    getMaterialGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-purchase-edit-list-material-grid',
            service: 'limsPurchaseMaterialServiceImpl',
            dbTable: 'T_LIMS_PURCHASE_MATERIAL',
            columns: limsPurchase.getMaterialShowListColumns(),
            deleteFormatter: function (row) {
                return row.code || '第' + (row.index + 1) + '行'
            },
            url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                id: _this.param.id
            }),
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add-row',
                hidden: !_this.param.step || _this.param.step !== 'edit',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsPurchase.getMaterialBaseInfoForm(),
                        disableNotEmptyValidate: true,
                        url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials', {
                            id: _this.param.id
                        }),
                        columns: 2,
                        width: 800,
                        height: 400
                    }).done(function (id) {
                        Gikam.getComp('lims-purchase-edit-list-material-grid').refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                hidden: !_this.param.step || _this.param.step !== 'edit',
                onClick: function () {
                    Gikam.getComp('lims-purchase-edit-list-material-grid').deleteRows(Gikam.printf(limsPurchase.baseUrl + '/{id}/materials', {
                        id: Gikam.getComp('lims-purchase-edit-list-grid').getActivedRow().id
                    }));
                }
            }],
        };
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    getAttachmantStruct: function (gridId, dbTable) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: !_this.param.step || _this.param.step !== 'edit',
            bizId: _this.param.id,
            onBeforeUpload: function () {
                if (Gikam.isEmpty(_this.param.id)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsPurchase.getFlowListColumns(),
            url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + _this.param.id
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-purchase-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getBtnToolbar(_this.param.id, _this.param.step), {
                    type: 'tab',
                    panels: [{
                        title: 'BASEMODULE.BASEDATA.PURCHASES.TAB.MATERIAL',
                        items: [this.getMaterialGrid()]
                    }, {
                        title: 'GIKAM.FILE.ATTACHMENT',
                        items: [_this.getAttachmantStruct(_this.idManager.trainRecordGrid, 'T_LIMS_PURCHASE')]
                    }, {
                        title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                        items: [_this.getAuditRecordGrid()]
                    }]
                }]
            }
        });
    },

    idManager: {
        trainRecordGrid: 'lims-purchase-edit-list-grid',
        auditRecordGrid: 'lims-purchase-edit-list-grid' + '-record',
    },

    init: function (param) {
        this.param = param
        this.create();
    }
};
